//
//  HCOAuthViewController.swift
//  Hweibome
//
//  Created by 何川 on 15/12/7.
//  Copyright © 2015年 hechuan. All rights reserved.
//

import UIKit
import SVProgressHUD


let WB_APPKEY = "3601631669"
let WB_APP_SECRET = "a7c5139465aa7bd3972a6f121e17fc03"
let WB_REDIRECT_URI = "http://www.hechuan.org.cn"




class HCOAuthViewController: UIViewController ,UIWebViewDelegate{

    private lazy var webView = UIWebView()
    
    override func loadView() {
        webView.delegate = self
        view = webView
    }
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        setupUI()
        let urlString = "https://api.weibo.com/oauth2/authorize?client_id=\(WB_APPKEY)&redirect_uri=\(WB_REDIRECT_URI)"
        let url = NSURL (string: urlString)!
        let request = NSURLRequest (URL: url)
        webView.loadRequest(request)
//https://api.weibo.com/oauth2/authorize?client_id=3601631669&redirect_uri=http://www.hechuan.org.cn
//https://api.weibo.com/oauth2/authorize?client_id=3601631669&redirect_uri=http://www.hechuan.org.cn
    }

    //MARK: - webview delegate
    
    func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool {
        guard let url = request.URL?.absoluteString  else {
            return true;
        }
        
        if !url.hasPrefix(WB_REDIRECT_URI)
        {
            return true;
        }
        
        if request.URL!.query!.hasPrefix("code="){
         //   printLog(request.URL!.query)
            let code = request.URL!.query!.substringFromIndex("code=".endIndex)
            HCUserAccountViewModel.sharedAccount.loadAccessToken(code, complete: { (isSuccessed) -> () in
                if !isSuccessed
                {
                        printLog("加载失败")
                    SVProgressHUD.showErrorWithStatus("网络异常,登录失败")
                    return
                }
                printLog("登录成功")
                
                SVProgressHUD.dismiss()
                
                self.dismissViewControllerAnimated(false, completion: { () -> Void in
                    NSNotificationCenter.defaultCenter().postNotificationName(HCSwitchRootVCNotifation, object: self)
                })
                
            })
            
        } else
        {
           // printLog(request.URL)
            dismiss()
        }

        return false
    }
    

    
    private func setupUI(){
        navigationItem.leftBarButtonItem = UIBarButtonItem(title: "取消", target: self, action: "dismiss")
        navigationItem.rightBarButtonItem = UIBarButtonItem(title: "自动填充", target: self, action: "autoFill")
        navigationItem.title = "微博登录"
    }
    
    func webViewDidStartLoad(webView: UIWebView) {
        SVProgressHUD.show()
    }
    
    func webViewDidFinishLoad(webView: UIWebView) {
        SVProgressHUD.dismiss()
    }
    func webView(webView: UIWebView, didFailLoadWithError error: NSError?) {
        SVProgressHUD.dismiss()
    }
    @objc private func dismiss()
    {
        SVProgressHUD.dismiss()
        dismissViewControllerAnimated(true, completion: nil)
    }
    @objc private func autoFill()
    {
        printLog("autoFill")
        
        let jsString = "document.getElementById('userId').value='hccgk@sina.com';document.getElementById('passwd').value='521125qqqqqq'"
//         let jsString = "document.getElementById('userId').value='yohtr35601@163.com';document.getElementById('passwd').value='qw1987'"
        webView.stringByEvaluatingJavaScriptFromString(jsString)
        
        
    }
    
    

    

}

extension HCOAuthViewController
{
    
}
